package com.demo.widget.meis.scale;

import com.meis.widget.photodrag.PhotoDragHelper;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.utils.Matrix;

public class AppPhotoDragHelper extends PhotoDragHelper {
    private float mPreviousV;
    private AppOnDragListener mListener;

    @Override
    public void onDrag(float dy) {
        if (mListener != null && mListener.getDragView() != null && mListener.getMatrix() != null) {
            Component dragView = mListener.getDragView();
            Matrix matrix = mListener.getMatrix();
            matrix.postTranslate(0, dy);
            dragView.invalidate();
            setAlpha(matrix);
        }
    }

    @Override
    public void onRelease() {
        if (mListener != null && mListener.getDragView() != null && mListener.getMatrix() != null) {
            Component dragView = mListener.getDragView();
            Matrix matrix = mListener.getMatrix();
            float from = matrix.getData()[5];
            float to = mListener.getOriginY();
            final boolean isExit = Math.abs(from - to) > mDragSlopHeight;
            if (isExit) {
                mListener.onAnimationEnd(true);
            } else {
                endAnimation();
                mBackOrExitAnimator = new AnimatorValue();
                mBackOrExitAnimator.setDuration(mDuration);
                mBackOrExitAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                    @Override
                    public void onUpdate(AnimatorValue animatorValue, float v) {
                        float animatedValue = (to - from) * (v - mPreviousV);
                        mPreviousV = v;
                        matrix.postTranslate(0, animatedValue);
                        dragView.invalidate();
                        setAlpha(matrix);
                    }
                });
                mBackOrExitAnimator.setStateChangedListener(new SimpleAnimatorStateChangedListener() {
                    @Override
                    public void onEnd(Animator animator) {
                        mPreviousV = 0;
                    }
                });
                mBackOrExitAnimator.start();
            }
        }
    }

    private abstract static class SimpleAnimatorStateChangedListener implements Animator.StateChangedListener {

        @Override
        public void onStart(Animator animator) {
        }

        @Override
        public void onStop(Animator animator) {
        }

        @Override
        public void onCancel(Animator animator) {
        }

        @Override
        public void onPause(Animator animator) {
        }

        @Override
        public void onResume(Animator animator) {
        }
    }

    /**
     * 设置页面透明度
     *
     * @param matrix Y移动偏移量，为最终的Y坐标值（拖拽会实时更新）减去原始的Y坐标值（最初进页面时居中时的Y坐标）
     */
    private void setAlpha(Matrix matrix) {
        if (mListener != null) {
            float offsetY = matrix.getData()[5] - mListener.getOriginY();
            float allFadeY = mDragSlopHeight * 2;
            float offsetTranslationY = Math.abs(offsetY);
            if (offsetTranslationY > allFadeY) {
                offsetTranslationY = allFadeY;
            }
            mListener.onAlpha(1 - offsetTranslationY / allFadeY);
        }
    }

    @Override
    public PhotoDragHelper setOnDragListener(OnDragListener listener) {
        super.setOnDragListener(listener);
        this.mListener = (AppOnDragListener) listener;
        return this;
    }

    public interface AppOnDragListener extends OnDragListener {
        /**
         * 获取ScaleImage的Matrix
         *
         * @return ScaleImage的Matrix
         */
        Matrix getMatrix();

        /**
         * 获取ScaleImage最初始状态Y坐标
         *
         * @return ScaleImage最初始状态Y坐标Y坐标
         */
        float getOriginY();
    }
}
